package com.utils;
//日期范围校验

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;

@Slf4j
public class DateRangeUtil {

    private static final int DEFAULT_DAYS = 8;
    private static final int MAX_DAYS = 60;

    public static LocalDate[] getValidatedDateRange(LocalDate startDate, LocalDate endDate) throws IllegalArgumentException {

        log.info("正在进行日期校验:{},{}",startDate, endDate);
        //默认展示最近八天，【今天-1，昨天-7】
        if (startDate == null || endDate == null) {
            endDate = LocalDate.now().minusDays(1);//昨天
            startDate = endDate.minusDays(DEFAULT_DAYS-1); // 包含最近8天
            log.info("日期为空，设置为默认范围: {} 到 {}", startDate, endDate);
        }

        //开始日期是否晚于结束日期
        if (startDate.isAfter(endDate)) {
            log.error("开始日期晚于结束日期");
            throw new IllegalArgumentException("开始日期晚于结束日期");
        }

        //日期范围不能超过六十天//LocalDate.now().minusDays(1)
        if (endDate.isAfter(startDate.plusDays(MAX_DAYS))) {
            log.error("日期范围超过六十天");
            throw new IllegalArgumentException("日期范围超过六十天");
        }

        log.info("日期校验通过");

        return new LocalDate[]{startDate, endDate};
    }
}